package com.cxb.manager;

import java.lang.reflect.Method;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.telephony.SignalStrength;
import android.telephony.TelephonyManager;

/**
 * @ClassName: PhoneSignalInfo
 * @Description: 获取手机信号相关信息
 * @date 2013-9-22 下午4:06:40
 */
public class PhoneSignalInfo {

	public static final int SIGNAL_STRENGTH_NONE_OR_UNKNOWN = 0;
	public static final int SIGNAL_STRENGTH_POOR = 1;
	public static final int SIGNAL_STRENGTH_MODERATE = 2;
	public static final int SIGNAL_STRENGTH_GOOD = 3;
	public static final int SIGNAL_STRENGTH_GREAT = 4;
	public static final int NUM_SIGNAL_STRENGTH_BINS = 5;

	/**
	 * @Title: getMobileOperator
	 * @Description: 获取运营商信息
	 * @param @param context
	 * @param @return 0=获取失败。1=中国移动，2=中国联通，3=中国电信，4=未知
	 * @return int 返回类型
	 * @throws
	 */
	public static int getMobileOperator(Context context) {
		TelephonyManager telManager = (TelephonyManager) context
				.getSystemService(Context.TELEPHONY_SERVICE);
		String operator = telManager.getSimOperator();
		if (telManager.getSimState() == TelephonyManager.SIM_STATE_ABSENT
				|| telManager.getSimState() == TelephonyManager.SIM_STATE_UNKNOWN) {
			return 0;
		}
		if (operator != null) {
			if (operator.equals("46000") || operator.equals("46002")
					|| operator.equals("46007")) {
				return 1;
			} else if (operator.equals("46001") || operator.equals("46006")) {
				return 2;
			} else if (operator.equals("46003")) {
				return 3;
			} else {
				return 4;
			}
		}
		return 0;
	}

	/**
	 * @Title: getSignalStrength
	 * @Description: TODO 获取手机信号强度
	 * @param @param mSignalStrength
	 * @param @return 设定文件
	 * @return int 返回信号强度格数
	 * @throws
	 */
	public static int getSignalStrength(SignalStrength mSignalStrength,
			Context context) {
		try {
			int mStrengthLevel = 0;
			TelephonyManager telManager = (TelephonyManager) context
					.getSystemService(Context.TELEPHONY_SERVICE);
			int simState = telManager.getSimState();
			if (simState == TelephonyManager.SIM_STATE_ABSENT
					|| simState == TelephonyManager.SIM_STATE_UNKNOWN) {
				return 0;
			}
			if (mSignalStrength == null) {
				return 0;
			}

			Method getEvdoDbm = SignalStrength.class.getMethod("getEvdoDbm");
			int mEvdoDbm = (Integer) getEvdoDbm.invoke(mSignalStrength, null);
			if (mSignalStrength != null && mSignalStrength.isGsm()) {
				mStrengthLevel = getLteLevel(mSignalStrength);
				if ((mEvdoDbm == -7)) {
					mStrengthLevel = getGsmLevel(mSignalStrength);
				}
			} else {
				mStrengthLevel = getCdmaLevel(mSignalStrength);
			}
			return mStrengthLevel;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return 0;
	}

	/**
	 * @Title: getGsmLevel
	 * @Description: 获取GSM信号强度
	 * @param @param mSignalStrength
	 * @param @return 设定文件
	 * @return int 返回类型
	 * @throws
	 */

	private static int getGsmLevel(SignalStrength mSignalStrength) {
		int asu = mSignalStrength.getGsmSignalStrength();
		int mStrengthLevel = 0;
		// if (asu <= 2 || asu == 99)
		if (asu <= 2)
			mStrengthLevel = 0;
		else if (asu >= 12)
			mStrengthLevel = 5;
		else if (asu >= 10)
			mStrengthLevel = 4;
		else if (asu >= 8)
			mStrengthLevel = 3;
		else if (asu >= 5)
			mStrengthLevel = 2;
		else
			mStrengthLevel = 1;

		return mStrengthLevel;
	}

	/**
	 * @Title: getCdmaLevel
	 * @Description: 获取CDMA信号强度
	 * @param @param mSignalStrength
	 * @param @return 设定文件
	 * @return int 返回类型
	 * @throws
	 */
	private static int getCdmaLevel(SignalStrength mSignalStrength) {
		final int cdmaDbm = mSignalStrength.getCdmaDbm();
		final int cdmaEcio = mSignalStrength.getCdmaEcio();
		int levelDbm = 0;
		int levelEcio = 0;

		if (cdmaDbm >= -75)
			levelDbm = 5;
		else if (cdmaDbm >= -80)
			levelDbm = 4;
		else if (cdmaDbm >= -85)
			levelDbm = 3;
		else if (cdmaDbm >= -95)
			levelDbm = 2;
		else if (cdmaDbm >= -100)
			levelDbm = 1;
		else
			levelDbm = 0;

		// Ec/Io are in dB*10
		if (cdmaEcio >= -90)
			levelEcio = 4;
		else if (cdmaEcio >= -105)
			levelEcio = 4;
		else if (cdmaEcio >= -120)
			levelEcio = 3;
		else if (cdmaEcio >= -135)
			levelEcio = 2;
		else if (cdmaEcio >= -150)
			levelEcio = 1;
		else
			levelEcio = 0;

		return (levelDbm < levelEcio) ? levelDbm : levelEcio;
	}

	/**
	 * Get LTE as level 0..4
	 * 
	 * 陈伟斌
	 */
	public static int getLteLevel(SignalStrength mSignalStrength) {

		try {

			/*
			 * TS 36.214 Physical Layer Section 5.1.3 TS 36.331 RRC RSSI =
			 * received signal + noise RSRP = reference signal dBm RSRQ =
			 * quality of signal dB= Number of Resource blocksxRSRP/RSSI SNR =
			 * gain=signal/noise ratio = -10log P1/P2 dB
			 */
			int rssiIconLevel = SIGNAL_STRENGTH_NONE_OR_UNKNOWN;
			int rsrpIconLevel = -1;
			int snrIconLevel = -1;
			int mLteRsrp = -1;
			int mLteRssnr = -1;
			int mLteSignalStrength = -1;
			Method getLteRsrp = SignalStrength.class.getMethod("getLteRsrp");
			mLteRsrp = (Integer) getLteRsrp.invoke(mSignalStrength, null);
			Method getLteRssnr = SignalStrength.class.getMethod("getLteRssnr");
			mLteRssnr = (Integer) getLteRssnr.invoke(mSignalStrength, null);
			Method getLteSignalStrength = SignalStrength.class
					.getMethod("getLteSignalStrength");
			mLteRssnr = (Integer) getLteSignalStrength.invoke(mSignalStrength,
					null);
			if (mLteRsrp > -44) {
				rsrpIconLevel = -1;
			} else if (mLteRsrp >= -85) {
				rsrpIconLevel = SIGNAL_STRENGTH_GREAT;
			} else if (mLteRsrp >= -95) {
				rsrpIconLevel = SIGNAL_STRENGTH_GOOD;
			} else if (mLteRsrp >= -105) {
				rsrpIconLevel = SIGNAL_STRENGTH_MODERATE;
			} else if (mLteRsrp >= -115) {
				rsrpIconLevel = SIGNAL_STRENGTH_POOR;
			} else if (mLteRsrp >= -140) {
				rsrpIconLevel = SIGNAL_STRENGTH_NONE_OR_UNKNOWN;
			}

			/*
			 * Values are -200 dB to +300 (SNR*10dB) RS_SNR >= 13.0 dB =>4 bars
			 * 4.5 dB <= RS_SNR < 13.0 dB => 3 bars 1.0 dB <= RS_SNR < 4.5 dB =>
			 * 2 bars -3.0 dB <= RS_SNR < 1.0 dB 1 bar RS_SNR < -3.0 dB/No
			 * Service Antenna Icon Only
			 */
			if (mLteRssnr > 300) {
				snrIconLevel = -1;
			} else if (mLteRssnr >= 130) {
				snrIconLevel = SIGNAL_STRENGTH_GREAT;
			} else if (mLteRssnr >= 45) {
				snrIconLevel = SIGNAL_STRENGTH_GOOD;
			} else if (mLteRssnr >= 10) {
				snrIconLevel = SIGNAL_STRENGTH_MODERATE;
			} else if (mLteRssnr >= -30) {
				snrIconLevel = SIGNAL_STRENGTH_POOR;
			} else if (mLteRssnr >= -200) {
				snrIconLevel = SIGNAL_STRENGTH_NONE_OR_UNKNOWN;
			}

			/* Choose a measurement type to use for notification */
			if (snrIconLevel != -1 && rsrpIconLevel != -1) {
				/*
				 * The number of bars displayed shall be the smaller of the bars
				 * associated with LTE RSRP and the bars associated with the LTE
				 * RS_SNR
				 */
				return (rsrpIconLevel < snrIconLevel ? rsrpIconLevel
						: snrIconLevel);
			}

			if (snrIconLevel != -1) {
				return snrIconLevel;
			}

			if (rsrpIconLevel != -1) {
				return rsrpIconLevel;
			}

			/* Valid values are (0-63, 99) as defined in TS 36.331 */
			if (mLteSignalStrength > 63) {
				rssiIconLevel = SIGNAL_STRENGTH_NONE_OR_UNKNOWN;
			} else if (mLteSignalStrength >= 12) {
				rssiIconLevel = SIGNAL_STRENGTH_GREAT;
			} else if (mLteSignalStrength >= 8) {
				rssiIconLevel = SIGNAL_STRENGTH_GOOD;
			} else if (mLteSignalStrength >= 5) {
				rssiIconLevel = SIGNAL_STRENGTH_MODERATE;
			} else if (mLteSignalStrength >= 0) {
				rssiIconLevel = SIGNAL_STRENGTH_POOR;
			}

			return rssiIconLevel;

		} catch (Exception e) {
			e.printStackTrace();
		}
		return 0;
	}

	/**
	 * @Title: getNetworkType
	 * @Description: 返回网络类型,0=没有网络,1=wifi,2=3G,3=2G
	 * @param @param context
	 * @param @return 设定文件
	 * @return int 返回类型
	 */
	public static int getNetworkType(Context context) {
		ConnectivityManager manager = (ConnectivityManager) context
				.getApplicationContext().getSystemService(
						Context.CONNECTIVITY_SERVICE);
		if (manager == null) {
			return 0;
		}
		NetworkInfo networkinfo = manager.getActiveNetworkInfo();
		if (networkinfo == null || !networkinfo.isAvailable()) {
			return 0;
		} else if (networkinfo.getType() == ConnectivityManager.TYPE_WIFI) {
			return 1;
		} else if (networkinfo.getSubtype() == TelephonyManager.NETWORK_TYPE_EVDO_0
				|| networkinfo.getSubtype() == TelephonyManager.NETWORK_TYPE_EVDO_A
				|| networkinfo.getSubtype() == TelephonyManager.NETWORK_TYPE_EVDO_B
				|| networkinfo.getSubtype() == TelephonyManager.NETWORK_TYPE_HSUPA
				|| networkinfo.getSubtype() == TelephonyManager.NETWORK_TYPE_HSPA
				|| networkinfo.getSubtype() == TelephonyManager.NETWORK_TYPE_HSPAP
				|| networkinfo.getSubtype() == TelephonyManager.NETWORK_TYPE_HSDPA) {
			return 2;
		} else if (networkinfo.getSubtype() == TelephonyManager.NETWORK_TYPE_GPRS
				|| networkinfo.getSubtype() == TelephonyManager.NETWORK_TYPE_CDMA
				|| networkinfo.getSubtype() == TelephonyManager.NETWORK_TYPE_EDGE) {
			return 3;
		} else if (networkinfo.getSubtype() == TelephonyManager.NETWORK_TYPE_LTE) {
			return 4;
		} else {
			return 0;
		}
	}
}